<template>
  <div class="video-item" ref="videoItemRef">
    <div class="video-cover">
      <Cover :source="data.videoCover" />
      <div class="duration">
        {{ convertSecondsToHMS(data.duration) }}
      </div>
    </div>
    <div class="video-info">
      <div class="video-name">
        <span v-if="data.status != 3">{{ data.videoName }}</span>
        <router-link v-else :to="`/video/${data.videoId}`" class="a-link" target="_blank">{{
          data.videoName
        }}</router-link>
        <span v-if="data.status == 0 || data.status == 2" class="status waiting">{{
          data.statusName
        }}</span>
        <span v-if="data.status == 1 || data.status == 4" class="status fail">{{
          data.statusName
        }}</span>
      </div>
      <div class="video-item">{{ data.createTime }}</div>
      <div class="video-count">
        <span class="iconfont icon-play-solid">{{ data.playCount || 0 }}</span>
        <span class="iconfont icon-like-solid">{{ data.likeCount || 0 }}</span>
        <span class="iconfont icon-danmu-solid">{{ data.danmuCount || 0 }}</span>
        <span class="iconfont icon-comment-solid">{{ data.commentCount || 0 }}</span>
        <span class="iconfont icon-toubi-solid">{{ data.coinCount || 0 }}</span>
        <span class="iconfont icon-collection-solid">{{ data.collectCount || 0 }}</span>
      </div>
    </div>
    <div class="op-panel">
      <div
        :class="['btn edit-btn', [0, 2].includes(data.status) ? 'disable' : '']"
        @click="jump('editVideo')"
      >
        <span class="iconfont icon-edit">编辑</span>
      </div>
      <el-popover
        class="cust-popover"
        placement="bottom-end"
        :width="300"
        trigger="hover"
        :show-arrow="false"
      >
        <template #reference>
          <div class="btn more-btn">
            <div class="iconfont icon-more"></div>
          </div>
        </template>
        <div class="more-edit-panel">
          <div class="more-btn-list">
            <div
              :class="['more-edit-item', [0, 2].includes(data.status) ? 'disable' : '']"
              @click="jump('editVideo')"
            >
              <div class="iconfont icon-edit"></div>
              <div class="title">编辑稿件</div>
            </div>
            <div
              :class="['more-edit-item', [0, 2].includes(data.status) ? 'disable' : '']"
              @click="jump('danmu')"
            >
              <div class="iconfont icon-danmu"></div>
              <div class="title">弹幕管理</div>
            </div>
            <div
              :class="['more-edit-item', [0, 2].includes(data.status) ? 'disable' : '']"
              @click="jump('comment')"
            >
              <div class="iconfont icon-hudong"></div>
              <div class="title">评论管理</div>
            </div>
            <div class="interaction-op">
              <el-checkbox-group v-model="interactionInfo" @change="saveInteractionInfo">
                <el-checkbox value="1">关闭弹幕</el-checkbox>
                <el-checkbox value="0">关闭评论</el-checkbox>
              </el-checkbox-group>
            </div>
          </div>
          <div class="del-btn">
            <span class="iconfont icon-delete2" @click="deleteVideo">删除稿件</span>
          </div>
        </div>
      </el-popover>
    </div>
  </div>
</template>

<script setup lang="ts">
import {
  saveVideoInteractionHttpRequest,
  ucenterDeleteVideoHttpRequest
} from '@/services/api/ucenter'
import { confirmMessageBox } from '@/utils/confirm'
import message from '@/utils/message'
import { convertSecondsToHMS } from '@/utils/utilsFun'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const props = withDefaults(
  defineProps<{
    data: any
  }>(),
  {}
)
const router = useRouter()

const jumpURL = {
  editVideo: '/ucenter/editVideo',
  danmu: '/ucenter/danmu',
  comment: '/ucenter/comment'
} as const
type JumpURLKey = keyof typeof jumpURL
//跳转对应的稿件
const jump = (path: JumpURLKey) => {
  router.push(`${jumpURL[path] as string}?videoId=${props.data.videoId}`)
}

const emit = defineEmits(['reload'])
//删除稿件
const deleteVideo = () => {
  confirmMessageBox({
    message: `确认要删除【${props.data.videoName}吗？】`,
    onConfirm: async () => {
      const res = await ucenterDeleteVideoHttpRequest(props.data.videoId)
      if (!res) {
        return
      }
      message.success('删除成功')
      emit('reload')
    },
    onCancel: () => {
      console.log('取消')
    }
  })
}

//保存视频互动信息
const interactionInfo = ref(props.data.interactionInfo ? props.data.interactionInfo.split(',') : [])
const saveInteractionInfo = async (e: any) => {
  const res = await saveVideoInteractionHttpRequest({
    videoId: props.data.videoId,
    interaction: e.join(',')
  })
  if (!res) {
    return
  }
  console.log(res, '保存成功')
}
</script>

<style lang="less" scoped>
.video-item {
  display: flex;
  border-bottom: 1px solid #ddd;
  padding: 30px 0;
  align-items: center;
  .video-cover {
    min-width: 0;
    width: 155px;
    position: relative;
    .duration {
      position: absolute;
      right: 0;
      bottom: 0;
      padding: 3px;
      border-radius: 5px 0px 5px 0px;
      background: rgba(0, 0, 0, 0.7);
      opacity: 0.8;
      color: #fff;
      font-size: 13px;
    }
  }
  .video-info {
    flex: 1;
    margin: 0 20px;
    .video-name {
      font-size: 16px;
      .status {
        font-size: 14px;
        margin-left: 5px;
      }
      .waiting {
        color: #e6a23c;
      }
      .fail {
        color: #f56c6c;
      }
      .sucess {
        color: #67c23a;
      }
    }
    .video-item {
      margin: 15px 0px;
      border-bottom: none;
      color: var(--text2);
      padding: 0;
    }
    .video-count {
      margin-top: 10px;
      color: var(--text3);
      display: flex;
      align-items: center;
      .iconfont {
        font-size: 14px;
        margin-right: 20px;
        &::before {
          font-size: 18px;
          margin-right: 5px;
        }
      }
    }
  }
  .op-panel {
    width: 120px;
    height: 33px;
    line-height: 33px;
    display: flex;
    justify-content: space-between;
    .btn {
      cursor: pointer;
      border: 1px solid #ddd;
      padding: 0px 8px;
      color: var(--text2);
      &:hover {
        border: 1px solid var(--blue);
      }
    }
    .icon-edit {
      font-size: 14px;
      margin-right: 5px;
      margin-left: 5px;
    }
  }
}

.more-edit-panel {
  .more-btn-list {
    border-bottom: 1px solid #ddd;
    padding-bottom: 10px;
    display: flex;
    flex-wrap: wrap;
    .more-edit-item {
      text-align: center;
      cursor: pointer;
      width: 33%;
      .title {
        font-size: 12px;
        margin-top: 5px;
      }
      &:hover {
        color: var(--blue);
      }
    }
    .interaction-op {
      width: 200px;
      margin-top: 20px;
      padding-left: 20px;
      :deep(.el-checkbox__label) {
        font-size: 12px;
      }
    }
  }
  .del-btn {
    text-align: center;
    margin: 10px 0px 5px 0px;
    cursor: pointer;
    .iconfont {
      font-size: 13px;
    }
    &:hover {
      color: var(--blue);
    }
  }
}
</style>
